#include "mswin/dir_lock.h"
#define WIN32_LEAN_AND_MEAN
#include <windows.h>

namespace mswin {

bool dir_lock::lock(const char * path)
{
	unlock();
	handle_ = CreateFile(
		path,
		FILE_ALL_ACCESS,
		0, //exclusive ownership
		0,
		OPEN_ALWAYS,
		FILE_FLAG_BACKUP_SEMANTICS, //the flag is required for directory opening
		0);
	if (handle_ == INVALID_HANDLE_VALUE) {
		handle_ = 0;
		return false;
	}
	return true;
}

void dir_lock::unlock()
{
	if (handle_) {
		CloseHandle(handle_);
		handle_ = 0;
	}
}

} //namespace mswin
